home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ohlutil / glob.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-24  |  14.6 KB  |  618 lines

  1. /* File-name wildcard pattern matching for GNU.
  2.    Copyright (C) 1985, 1988, 1989, 1990 Free Software Foundation, Inc.
  3.    ChangeLog: changelog
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 1, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/fileutil/RCS/glob.c'v 1.3.0.2 90/06/29 00:46:48 tho Stable $
  34.  */
  35.  
  36. /* To whomever it may concern: I have never seen the code which most
  37.    Unix programs use to perform this function.  I wrote this from scratch
  38.    based on specifications for the pattern matching.  --RMS.  */
  39.  
  40. #ifdef MSDOS
  41.  
  42. #include <stdio.h>
  43. #include <sys/types.h>
  44. #include <malloc.h>
  45. #include "msd_dir.h"
  46. #define D_NAMLEN(d) ((d)->d_namlen)
  47.  
  48. #else /* not MSDOS */
  49.  
  50. #if defined(USGr3) || defined(DIRENT) || defined(__GNU_LIBRARY__)
  51. #include <dirent.h>
  52. #define direct dirent
  53. #define D_NAMLEN(d) strlen((d)->d_name)
  54. #else /* not USGr3 or DIRENT or __GNU_LIBRARY__ */
  55. #define D_NAMLEN(d) ((d)->d_namlen)
  56. #ifdef USG
  57. #ifdef SYSNDIR
  58. #include <sys/ndir.h>
  59. #else
  60. #include "ndir.h"        /* Get ndir.h from the Emacs distribution.  */
  61. #endif /* not SYSNDIR */
  62. #else /* not USG */
  63. #include <sys/dir.h>
  64. #endif /* USG */
  65. #endif /* USGr3 */
  66.  
  67. #endif /* not MSDOS */
  68.  
  69. #if    defined(STDC_HEADERS) || defined(__GNU_LIBRARY__)
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  73. #define index strchr
  74. #define rindex strrchr
  75. #else
  76.  
  77. #ifdef USG
  78. #include <string.h>
  79. #include <memory.h>
  80. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  81. #define index strchr
  82. #define rindex strrchr
  83. #else /* not USG */
  84. #include <strings.h>
  85. extern void bcopy ();
  86. #endif /* not USG */
  87.  
  88. extern char *malloc ();
  89. extern char *realloc ();
  90. extern void free ();
  91.  
  92. #ifndef NULL
  93. #define NULL 0
  94. #endif
  95. #endif    /* Not STDC_HEADERS or __GNU_LIBRARY__.  */
  96.  
  97. #ifdef __GNUC__
  98. #define alloca __builtin_alloca
  99. #else /* Not GCC.  */
  100. #ifdef sparc
  101. #include <alloca.h>
  102. #else /* Not sparc.  */
  103. #ifndef MSDOS
  104. extern char *alloca ();
  105. #endif /* not MSDOS */
  106. #endif /* sparc.  */
  107. #endif /* GCC.  */
  108.  
  109. #ifdef MSDOS
  110. extern  int glob_pattern_p(char *);
  111. extern  int glob_match(char *,char *,int);
  112. static  int glob_match_after_star(char *,char *);
  113. extern  char * *glob_vector(char *,char *);
  114. static  char * *glob_dir_to_array(char *,char **);
  115. extern  char * *glob_filename(char *);
  116. #endif /* MSDOS */
  117.   
  118.  
  119. /* Nonzero if '*' and '?' do not match an initial '.' for glob_filename.  */
  120. int noglob_dot_filenames = 1;
  121.  
  122. static int glob_match_after_star ();
  123.  
  124. /* Return nonzero if PATTERN has any special globbing chars in it.  */
  125.  
  126. int
  127. glob_pattern_p (pattern)
  128.      char *pattern;
  129. {
  130.   register char *p = pattern;
  131.   register char c;
  132.  
  133.   while ((c = *p++) != '\0')
  134.     switch (c)
  135.       {
  136.       case '?':
  137.       case '[':
  138.       case '*':
  139.     return 1;
  140.  
  141.       case '\\':
  142.     if (*p++ == '\0')
  143.       return 0;
  144.       }
  145.  
  146.   return 0;
  147. }
  148.  
  149.  
  150. /* Match the pattern PATTERN against the string TEXT;
  151.    return 1 if it matches, 0 otherwise.
  152.  
  153.    A match means the entire string TEXT is used up in matching.
  154.  
  155.    In the pattern string, `*' matches any sequence of characters,
  156.    `?' matches any character, [SET] matches any character in the specified set,
  157.    [!SET] matches any character not in the specified set.
  158.  
  159.    A set is composed of characters or ranges; a range looks like
  160.    character hyphen character (as in 0-9 or A-Z).
  161.    [0-9a-zA-Z_] is the set of characters allowed in C identifiers.
  162.    Any other character in the pattern must be matched exactly.
  163.  
  164.    To suppress the special syntactic significance of any of `[]*?!-\',
  165.    and match the character exactly, precede it with a `\'.
  166.  
  167.    If DOT_SPECIAL is nonzero,
  168.    `*' and `?' do not match `.' at the beginning of TEXT.  */
  169.  
  170. int
  171. glob_match (pattern, text, dot_special)
  172.      char *pattern, *text;
  173.      int dot_special;
  174. {
  175.   register char *p = pattern, *t = text;
  176.   register char c;
  177.  
  178.   while ((c = *p++) != '\0')
  179.     switch (c)
  180.       {
  181.       case '?':
  182.     if (*t == '\0' || (dot_special && t == text && *t == '.'))
  183.       return 0;
  184.     else
  185.       ++t;
  186.     break;
  187.  
  188.       case '\\':
  189.     if (*p++ != *t++)
  190.       return 0;
  191.     break;
  192.  
  193.       case '*':
  194.     if (dot_special && t == text && *t == '.')
  195.       return 0;
  196.     return glob_match_after_star (p, t);
  197.  
  198.       case '[':
  199.     {
  200.       register char c1 = *t++;
  201.       int invert;
  202.  
  203.       if (c1 == '\0')
  204.         return 0;
  205.  
  206.       invert = (*p == '!');
  207.  
  208.       if (invert)
  209.         p++;
  210.  
  211.       c = *p++;
  212.       while (1)
  213.         {
  214.           register char cstart = c, cend = c;
  215.  
  216.           if (c == '\\')
  217.         {
  218.           cstart = *p++;
  219.           cend = cstart;
  220.         }
  221.  
  222.           if (cstart == '\0')
  223.         return 0;    /* Missing ']'. */
  224.  
  225.           c = *p++;
  226.  
  227.           if (c == '-')
  228.         {
  229.           cend = *p++;
  230.           if (cend == '\\')
  231.             cend = *p++;
  232.           if (cend == '\0')
  233.             return 0;
  234.           c = *p++;
  235.         }
  236.           if (c1 >= cstart && c1 <= cend)
  237.         goto match;
  238.           if (c == ']')
  239.         break;
  240.         }
  241.       if (!invert)
  242.         return 0;
  243.       break;
  244.  
  245.     match:
  246.       /* Skip the rest of the [...] construct that already matched.  */
  247.       while (c != ']')
  248.         {
  249.           if (c == '\0')
  250.         return 0;
  251.           c = *p++;
  252.           if (c == '\0')
  253.         return 0;
  254.           if (c == '\\')
  255.         p++;
  256.         }
  257.       if (invert)
  258.         return 0;
  259.       break;
  260.     }
  261.  
  262.       default:
  263.     if (c != *t++)
  264.       return 0;
  265.       }
  266.  
  267.   return *t == '\0';
  268. }
  269.  
  270. /* Like glob_match, but match PATTERN against any final segment of TEXT.  */
  271.  
  272. static int
  273. glob_match_after_star (pattern, text)
  274.      char *pattern, *text;
  275. {
  276.   register char *p = pattern, *t = text;
  277.   register char c, c1;
  278.  
  279.   while ((c = *p++) == '?' || c == '*')
  280.     if (c == '?' && *t++ == '\0')
  281.       return 0;
  282.  
  283.   if (c == '\0')
  284.     return 1;
  285.  
  286.   if (c == '\\')
  287.     c1 = *p;
  288.   else
  289.     c1 = c;
  290.  
  291.   --p;
  292.   while (1)
  293.     {
  294.       if ((c == '[' || *t == c1) && glob_match (p, t, 0))
  295.     return 1;
  296.       if (*t++ == '\0')
  297.     return 0;
  298.     }
  299. }
  300.  
  301. /* Return a vector of names of files in directory DIR
  302.    whose names match glob pattern PAT.
  303.    The names are not in any particular order.
  304.    Wildcards at the beginning of PAT do not match an initial period
  305.    if noglob_dot_filenames is nonzero.
  306.  
  307.    The vector is terminated by an element that is a null pointer.
  308.  
  309.    To free the space allocated, first free the vector's elements,
  310.    then free the vector.
  311.  
  312.    Return NULL if cannot get enough memory to hold the pointer
  313.    and the names.
  314.  
  315.    Return -1 if cannot access directory DIR.
  316.    Look in errno for more information.  */
  317.  
  318. char **
  319. glob_vector (pat, dir)
  320.      char *pat;
  321.      char *dir;
  322. {
  323.   struct globval
  324.   {
  325.     struct globval *next;
  326.     char *name;
  327.   };
  328.  
  329.   DIR *d;
  330.   register struct direct *dp;
  331.   struct globval *lastlink;
  332.   register struct globval *nextlink;
  333.   register char *nextname;
  334.   unsigned int count;
  335.   int lose;
  336.   register char **name_vector;
  337.   register unsigned int i;
  338.  
  339.   d = opendir (dir);
  340.   if (d == NULL)
  341.     return (char **) -1;
  342.  
  343.   lastlink = NULL;
  344.   count = 0;
  345.   lose = 0;
  346.  
  347.   /* Scan the directory, finding all names that match.
  348.      For each name that matches, allocate a struct globval
  349.      on the stack and store the name in it.
  350.      Chain those structs together; lastlink is the front of the chain.  */
  351.   while (1)
  352.     {
  353.       dp = readdir (d);
  354.       if (dp == NULL)
  355.     break;
  356. #ifdef MSDOS    /* dp->d_ino is always 0 in the MS-DOS implementation. */
  357.       if (glob_match (pat, dp->d_name, noglob_dot_filenames))
  358. #else
  359.       if (dp->d_ino != 0
  360.       && glob_match (pat, dp->d_name, noglob_dot_filenames))
  361. #endif /* MSDOS */
  362.     {
  363.       nextlink = (struct globval *) alloca (sizeof (struct globval));
  364.       nextlink->next = lastlink;
  365.       i = D_NAMLEN (dp) + 1;
  366.       nextname = (char *) malloc (i);
  367.       if (nextname == NULL)
  368.         {
  369.           lose = 1;
  370.           break;
  371.         }
  372.       lastlink = nextlink;
  373.       nextlink->name = nextname;
  374.       bcopy (dp->d_name, nextname, i);
  375.       count++;
  376.     }
  377.     }
  378.   closedir (d);
  379.  
  380.   if (!lose)
  381.     {
  382.       name_vector = (char **) malloc ((count + 1) * sizeof (char *));
  383.       lose |= name_vector == NULL;
  384.     }
  385.  
  386.   /* Have we run out of memory?  */
  387.   if (lose)
  388.     {
  389.       /* Here free the strings we have got.  */
  390.       while (lastlink)
  391.     {
  392.       free (lastlink->name);
  393.       lastlink = lastlink->next;
  394.     }
  395.       return NULL;
  396.     }
  397.  
  398.   /* Copy the name pointers from the linked list into the vector.  */
  399.   for (i = 0; i < count; ++i)
  400.     {
  401.       name_vector[i] = lastlink->name;
  402.       lastlink = lastlink->next;
  403.     }
  404.  
  405.   name_vector[count] = NULL;
  406.   return name_vector;
  407. }
  408.  
  409. /* Return a new array, replacing ARRAY, which is the concatenation
  410.    of each string in ARRAY to DIR.
  411.    Return NULL if out of memory.  */
  412.  
  413. static char **
  414. glob_dir_to_array (dir, array)
  415.      char *dir, **array;
  416. {
  417.   register unsigned int i, l;
  418.   int add_slash = 0;
  419.   char **result;
  420.  
  421.   l = strlen (dir);
  422.   if (l == 0)
  423.     return array;
  424.  
  425.   if (dir[l - 1] != '/')
  426.     add_slash++;
  427.  
  428.   for (i = 0; array[i] != NULL; i++)
  429.     ;
  430.  
  431.   result = (char **) malloc ((i + 1) * sizeof (char *));
  432.   if (result == NULL)
  433.     return NULL;
  434.  
  435.   for (i = 0; array[i] != NULL; i++)
  436.     {
  437.       result[i] = (char *) malloc (1 + l + add_slash + strlen (array[i]));
  438.       if (result[i] == NULL)
  439.     return NULL;
  440.       strcpy (result[i], dir);
  441.       if (add_slash)
  442.     result[i][l] = '/';
  443.       strcpy (result[i] + l + add_slash, array[i]);
  444.     }
  445.   result[i] = NULL;
  446.  
  447.   /* Free the input array.  */
  448.   for (i = 0; array[i] != NULL; i++)
  449.     free (array[i]);
  450.   free ((char *) array);
  451.   return result;
  452. }
  453.  
  454. /* Do globbing on PATHNAME.  Return an array of pathnames that match,
  455.    marking the end of the array with a null-pointer as an element.
  456.    If no pathnames match, then the array is empty (first element is null).
  457.    If there isn't enough memory, then return NULL.
  458.    If a file system error occurs, return -1; `errno' has the error code.
  459.  
  460.    Wildcards at the beginning of PAT, or following a slash,
  461.    do not match an initial period if noglob_dot_filenames is nonzero.  */
  462.  
  463. char **
  464. glob_filename (pathname)
  465.      char *pathname;
  466. {
  467.   char **result;
  468.   unsigned int result_size;
  469.   char *directory_name, *filename;
  470.   unsigned int directory_len;
  471.  
  472.   result = (char **) malloc (sizeof (char *));
  473.   result_size = 1;
  474.   if (result == NULL)
  475.     return NULL;
  476.  
  477.   result[0] = NULL;
  478.  
  479.   /* Find the filename.  */
  480.   filename = rindex (pathname, '/');
  481.   if (filename == NULL)
  482.     {
  483.       filename = pathname;
  484.       directory_name = "";
  485.       directory_len = 0;
  486.     }
  487.   else
  488.     {
  489.       directory_len = (filename - pathname) + 1;
  490.       directory_name = (char *) alloca (directory_len + 1);
  491.       bcopy (pathname, directory_name, directory_len);
  492.       directory_name[directory_len] = '\0';
  493.       ++filename;
  494.     }
  495.  
  496.   /* If directory_name contains globbing characters, then we
  497.      have to expand the previous levels.  Just recurse. */
  498.   if (glob_pattern_p (directory_name))
  499.     {
  500.       char **directories;
  501.       register unsigned int i;
  502.  
  503.       if (directory_name[directory_len - 1] == '/')
  504.     directory_name[directory_len - 1] = '\0';
  505.  
  506.       directories = glob_filename (directory_name);
  507.       if (directories == NULL)
  508.     goto memory_error;
  509.       else if (directories == (char **) -1)
  510.     return (char **) -1;
  511.       else if (*directories == NULL)
  512.     {
  513.       free ((char *) directories);
  514.       return (char **) -1;
  515.     }
  516.  
  517.       /* We have successfully globbed the preceding directory name.
  518.      For each name in DIRECTORIES, call glob_vector on it and
  519.      FILENAME.  Concatenate the results together.  */
  520.       for (i = 0; directories[i] != NULL; i++)
  521.     {
  522.       char **temp_results = glob_vector (filename, directories[i]);
  523.       if (temp_results == NULL)
  524.         goto memory_error;
  525.       else if (temp_results == (char **) -1)
  526.         /* This filename is probably not a directory.  Ignore it.  */
  527.         ;
  528.       else
  529.         {
  530.           char **array = glob_dir_to_array (directories[i], temp_results);
  531.           register unsigned int l;
  532.  
  533.           l = 0;
  534.           while (array[l] != NULL)
  535.         ++l;
  536.  
  537.           result = (char **) realloc (result,
  538.                       (result_size + l) * sizeof (char *));
  539.           if (result == NULL)
  540.         goto memory_error;
  541.  
  542.           for (l = 0; array[l] != NULL; ++l)
  543.         result[result_size++ - 1] = array[l];
  544.           result[result_size - 1] = NULL;
  545.           free ((char *) array);
  546.         }
  547.     }
  548.       /* Free the directories.  */
  549.       for (i = 0; directories[i] != NULL; i++)
  550.     free (directories[i]);
  551.       free ((char *) directories);
  552.  
  553.       return result;
  554.     }
  555.  
  556.   /* If there is only a directory name, return it. */
  557.   if (*filename == '\0')
  558.     {
  559.       result = (char **) realloc ((char *) result, 2 * sizeof (char *));
  560.       if (result == NULL)
  561.     return NULL;
  562.       result[0] = (char *) malloc (directory_len + 1);
  563.       if (result[0] == NULL)
  564.     goto memory_error;
  565.       bcopy (directory_name, result[0], directory_len + 1);
  566.       result[1] = NULL;
  567.       return result;
  568.     }
  569.   else
  570.     {
  571.       /* Otherwise, just return what glob_vector
  572.      returns appended to the directory name. */
  573.       char **temp_results = glob_vector (filename,
  574.                      (directory_len == 0
  575.                       ? "." : directory_name));
  576.  
  577.       if (temp_results == NULL || temp_results == (char **) -1)
  578.     return temp_results;
  579.  
  580.       return glob_dir_to_array (directory_name, temp_results);
  581.     }
  582.  
  583. memory_error:;
  584.   if (result != NULL)
  585.     {
  586.       register unsigned int i;
  587.       for (i = 0; result[i] != NULL; ++i)
  588.     free (result[i]);
  589.       free ((char *) result);
  590.     }
  591.   return NULL;
  592. }
  593.  
  594. #ifdef TEST
  595.  
  596. main (argc, argv)
  597.      int argc;
  598.      char **argv;
  599. {
  600.   char **value;
  601.   int i, optind;
  602.  
  603.   for (optind = 1; optind < argc; optind++)
  604.     {
  605.       value = glob_filename (argv[optind]);
  606.       if (value == NULL)
  607.     puts ("virtual memory exhausted");
  608.       else if (value == (char **) -1)
  609.     perror (argv[optind]);
  610.       else
  611.     for (i = 0; value[i] != NULL; i++)
  612.       puts (value[i]);
  613.     }
  614.   exit (0);
  615. }
  616.  
  617. #endif /* TEST */
  618.